//
//  WJQUserInfoViewController.m
//  TripTogether
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 杨泓晔. All rights reserved.
//

#import "WJQUserInfoViewController.h"
#import <BmobSDK/Bmob.h>
#import <SMS_SDK/SMSSDK.h>

@interface WJQUserInfoViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTable;
@property (nonatomic, strong) NSMutableDictionary *myDic;

@end

@implementation WJQUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _myDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:self.infoMod.imageURL, @"头像", self.infoMod.userName, @"用户名", self.infoMod.phone, @"手机号码",nil];
    
    
    
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.title = @"个人信息";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(reightBarAction:)];
    
    [self createTableView];
    
    
    
}
// 点击完成
- (void)reightBarAction:(UIBarButtonItem *)sender
{
    
    [self dismissViewControllerAnimated:NO completion:^{
        
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"close" object:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refurbish" object:nil];
        
    }];
    
}
- (void)createTableView
{
    _myTable = [UITableView new];
    _myTable.dataSource = self;
    _myTable.delegate = self;
    [_myTable registerClass:[WJQUserInfoTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_myTable];
    
    [_myTable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(@0);
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20.f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section) {
        return 100;
    }
 
    return 44;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (0 == section) {
        return 1;
    }
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    
    WJQUserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    
    if (0 == indexPath.section) {
        [cell setInfoValueTitle:@"头像" subTitle:_myDic[@"头像"] isImageURL:YES];
    } else if (indexPath.section > 0) {
        if (0 == indexPath.row) {
            [cell setInfoValueTitle:@"用户名" subTitle:_myDic[@"用户名"] isImageURL:NO];
        } else {
            [cell setInfoValueTitle:@"手机号码" subTitle:_myDic[@"手机号码"] isImageURL:NO];
        }
    }
    
    
    
    
    
    
    //设置右边的小图标
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    BmobUser *bUser = [BmobUser getCurrentUser];
    if (bUser) {
        //进行操作
       
        if (0 == indexPath.section) {
            
            
        } else if (1 == indexPath.section) {
            if (0 == indexPath.row) {
                
                if ([bUser objectForKey:@"isEditUserName"]) {
                    
                    [self alertControllerWithTitle:@"提示" message:@"用名已经修改过了"];
                    
                } else {
                  [self editUserName:bUser];
                }
                
                
            } else if (1 == indexPath.row) {
                
            }
        }
        
        
    }else{
        
        [self alertControllerWithTitle:@"提示" message:@"网络连接错误"];
    }
    
   
}
- (void)editUserName:(BmobUser *)user
{
    
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"修改用户名" message:@"注意:用户名只能修改一次" preferredStyle:UIAlertControllerStyleAlert];
    
    
    [alertC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        textField.placeholder = @"请输入用户名";
        
    }];
    
    [self presentViewController:alertC animated:YES completion:^{
        
    }];
    // 创建提示框按钮
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // block回调
        NSLog(@"点击的是确定按钮");
        
        // 得到输入姓名的输入框
        UITextField *nameTextField = alertC.textFields[0];

#warning 检查用名名合法性
        if (0) {
            NSLog(@"不合法");
        } else {
            
            
            [self MBShow];
            BmobQuery *query = [BmobUser query];
            [query whereKey:@"username" equalTo:nameTextField.text];
            [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
                
                [self MBHide];
                if (array.count != 0) {
                    //                NSLog(@"用户名已被占用, 请重新输入");
                    
                    [self alertControllerWithTitle:@"用名名已被占用" message:@"请重新输入"];
                    
                } else {
                    
                    
                    [user setObject:nameTextField.text forKey:@"username"];
                    [user setObject:[NSNumber numberWithBool:YES] forKey:@"isEditUserName"];
                    
                    [user updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                        
                        if (isSuccessful) {
                            [self alertControllerWithTitle:@"提示" message:@"用户名修改成功!"];
                            [_myDic setObject:nameTextField.text forKey:@"用户名"];
                            [_myTable reloadData];
                        } else {
                            [self alertControllerWithTitle:@"提示" message:@"用户名修改失败!"];
                            NSLog(@"error %@",[error description]);
                        }
                        
                    }];
                }
            }];
            
            
        }
    
    }];
    UIAlertAction *bction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    // 将按钮放在提示框上
    [alertC addAction:bction];
    [alertC addAction:action];
  

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
